package cn.iocoder.yudao.module.nmkj.controller.app.useraccess;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.nmkj.controller.admin.useraccess.vo.UserAccessSaveReqVO;
import cn.iocoder.yudao.module.nmkj.service.useraccess.UserAccessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.TOO_MANY_REQUESTS;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户APP - 访客消息")
@RestController
@RequestMapping("/nmkj/user-access")
@Validated
public class AppUserAccessController {

    @Resource
    private UserAccessService userAccessService;

    @PostMapping("/create")
    @Operation(summary = "创建访客消息")
    public CommonResult<Long> createUserAccess(@Valid @RequestBody UserAccessSaveReqVO createReqVO, HttpServletRequest request, HttpServletResponse response) {
        //所有的访客消息，一律设置为待审核状态
        createReqVO.setId(null);
        createReqVO.setStatus(0);
        Long code = userAccessService.createUserAccess(createReqVO, request, response);
        CommonResult<Long> commonResult = success(code);
        if (code == -1L) { //编号不存在，失败状态，返回指定操作频繁数据
            commonResult.setCode(TOO_MANY_REQUESTS.getCode());
            commonResult.setMsg(TOO_MANY_REQUESTS.getMsg());
        }
        return commonResult;
    }

}
